Page({

  /**
   * 页面的初始数据
   */
  data: {
    dayStyle: [
      { month: 'current', day: new Date().getDate(), color: 'white', background: '#AAD4F5' },
      { month: 'current', day: new Date().getDate() + 1, color: 'white', background: '#84e7d0' }
    ],
    productStatus: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options,"日历的携带参数");
    this.dateInit();
    console.log(this.data.dayStyle);
    if (options.date) {
      console.log("选了日期了");
      this.setData({
        dayStyle: [
          { month: 'current', day: new Date().getDate(), color: 'white', background: '#AAD4F5' },
          { month: 'current', day: new Date((options.date).replace(/-/g, "/")).getDate(), color: 'white', background: '#84e7d0' }
        ]
      });
    }
    this.data.productStatus = options.productStatus;
    console.log(this.data.productStatus);
    console.log(this.data.dayStyle);
  },
  dateInit: function (setYear, setMonth) {
    //全部时间的月份都是按0~11基准，显示月份才+1 
    let dateArr = [];            //需要遍历的日历数组数据 
    let arrLen = 0;             //dateArr的数组长度 
    let now = setYear ? new Date(setYear, setMonth) : new Date();
    let year = setYear || now.getFullYear();
    let nextYear = 0;
    let month = setMonth || now.getMonth();         //没有+1方便后面计算当月总天数 
    let nextMonth = (month + 1) > 11 ? 1 : (month + 1);
    let startWeek = new Date(year + ',' + (month + 1) + ',' + 1).getDay();             //目标月1号对应的星期 
    let dayNums = new Date(year, nextMonth, 0).getDate();       //获取目标月有多少天 
    let obj = {};
    let num = 0; 
    let start_date = '';
    let myDate = new Date();
    let mymouth = parseInt(myDate.getMonth()) + parseInt(1);
    let nowday = myDate.getFullYear() + '-' + mymouth + '-' + myDate.getDate();
    var end_date='';
    var days=0;
    let changeDay = `dayStyle[1].day`;
    let changeBg = `dayStyle[1].background`;

    if (month + 1 > 11) {
      nextYear = year + 1;
      dayNums = new Date(nextYear, nextMonth, 0).getDate();
    }
    arrLen = startWeek + dayNums;
    for (let i = 0; i < arrLen; i++) {
      if (i >= startWeek) {
        num = i - startWeek + 1;
        obj = {
          isToday: '' + year + (month + 1) + num,
          dateNum: num,
          weight: 5
        }
        let start_date_temp = year+"-"+(month+1)+'-'+num;
        start_date = new Date(start_date_temp.replace(/-/g, "/"));
    
        console.log(start_date,"start_date666666666666666")
        end_date = new Date(nowday.replace(/-/g, "/"));
        days = start_date.getTime() - end_date.getTime();
        //转换成天数
        var ss = parseInt(days / (1000 * 60 * 60 * 24));
        if (ss < 0) {

        let temp = { month: 'current', day: num, color: 'white', background: '#C0C0C0' };
         this.data.dayStyle.push(temp);
        }
      } else {
        obj = {};
      }
      
    }
    // this.setData({
    //   dateArr: dateArr
    // })

    let nowDate = new Date();
    let nowYear = nowDate.getFullYear();
    let nowMonth = nowDate.getMonth() + 1;
    let nowWeek = nowDate.getDay();
    let getYear = setYear || nowYear;
    let getMonth = setMonth >= 0 ? (setMonth + 1) : nowMonth;

    if (nowYear == getYear && nowMonth == getMonth) {
      this.setData({
        isTodayWeek: true,
        todayIndex: nowWeek
      })
    } else {
      this.setData({
        isTodayWeek: false,
        todayIndex: -1
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
  // 点击日历具体某一天的事件
  // dayClick: function (event) {
  //   console.log(event.detail);
  // },
  //
  selectDate (e) {
    console.log(e);
  },
  //给点击的日期设置一个背景颜色
  dayClick: function (event) {
    console.log(event);
    let clickDay = event.detail.day;
    let changeDay = `dayStyle[1].day`;
    let changeBg = `dayStyle[1].background`;
    this.setData({
      [changeDay]: clickDay,
      [changeBg]: "#84e7d0"
    })
    let year = event.detail.year;
    let month = event.detail.month;
    let day = event.detail.day;
    let myDate = new Date();
    let clickday = year+'-'+ month+'-'+day;
    let mymouth = parseInt(myDate.getMonth()) + parseInt(1);
    let nowday = myDate.getFullYear() + '-' + mymouth+ '-' + myDate.getDate();
    //日期格式化
    var start_date = new Date(clickday.replace(/-/g, "/"));
    var end_date = new Date(nowday.replace(/-/g, "/"));
    //转成毫秒数，两个日期相减
    var days = start_date.getTime() - end_date.getTime();
    //转换成天数
    var ss = parseInt(days / (1000 * 60 * 60 * 24));
    //do something
    console.log("day = ", ss);

    if (ss<=0){
      wx.showToast({
        title: '请选择今天以后日期',
        icon: 'loading',
        duration: 2000
      });
      this.setData({
        [changeDay]: clickDay,
        [changeBg]: "#C0C0C0"
      })
      return;
    }
    let productStatus = this.data.productStatus;
    console.log("year", year)
    console.log("month", month)
    console.log("day", day)
    console.log("productStatus", productStatus)
    var s = year +"-"+month+"-"+day


  // debugger
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];
    prevPage.setData({
      cccc: s
    
    })
    wx.navigateBack({
      delta: 1,
    })



  


    // wx.redirectTo({
    //   url: `/pages/merchants/list/index?year=${year}&month=${month}&day=${day}&productStatus=${productStatus}`
    // });






  },
})